package bavaria.hightech.statement;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Locale;
import java.util.ResourceBundle;

import bavaria.hightech.banking.Account;
import bavaria.hightech.banking.Transaction;

public class StatementFormatterTextHtml implements IStatementFormatter {

	private StringBuffer sb;
	private boolean finished = false;
	ResourceBundle bundle;
	Locale currentLocale;
	NumberFormat numberFormatter;
	DateFormat dateFormatter;
	
	public StatementFormatterTextHtml(Locale currentLocale){
		this.currentLocale = currentLocale;
		bundle = ResourceBundle.getBundle("props/local", currentLocale);
		numberFormatter = NumberFormat.getCurrencyInstance(currentLocale);
		dateFormatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, 
				DateFormat.DEFAULT, currentLocale);
		sb = new StringBuffer(
				"<html>\n" +
				"<head>\n" +
				"<title>HTML</title\n" +
				"</head>\n" +
				"<body>\n");
	}
	
	public void setCurrentLocale(Locale currentLocale) {
		this.currentLocale = currentLocale;
	}

	@Override
	public void addAccountData(Account account) {
		sb.append(bundle.getString("KONTONUMMER") + ": " /*+bundle.getString("wert1")*/ 
				+ account.getAccountNumber()+ "<br>");
		sb.append(bundle.getString("INHABER") + ": " + account.getDepositor()+ "<br>");
		sb.append(bundle.getString("KONTOSTANDNEU") + ": " + numberFormatter.format(
				account.getAccountBalance())+ "<br> <br>");
		
	}

	@Override
	public void addTransaction(Transaction transaction) {
		sb.append(bundle.getString("DATUM") + ": " + dateFormatter.format(transaction.getTime()) + "<br>");
		sb.append(bundle.getString("GRUND") + ": " + transaction.getReason() + "<br>");
		sb.append(bundle.getString("BETRAG") + ": " + numberFormatter.format(transaction.getMoney().getValue()) 
				+ "<br> <br>");
		
	}

	@Override
	public void addAccountBalance(long accountBalance) {
		sb.append(bundle.getString("KONTOSTANDNEU") + ": " + numberFormatter.format(accountBalance) + "<br> <br>");
		
	}
	
	public String getFormattedText(){
		if(!finished){
			sb.append("</body>\n" + 
					  "</html>");
			finished = true;
		}
		return sb.toString();
	}
	
	
	
}